.TH std::basic_istream::seekg 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::basic_istream::seekg \- std::basic_istream::seekg

.SH Synopsis
   basic_istream& seekg( pos_type pos );                             \fB(1)\fP
   basic_istream& seekg( off_type off, std::ios_base::seekdir dir ); \fB(2)\fP

   Sets input position indicator of the current associated streambuf object.

   Before doing anything else, seekg clears eofbit. \fI(since C++11)\fP

   seekg behaves as UnformattedInputFunction, except that gcount() is not affected.
   After constructing and checking the sentry object,

   1) if fail() != true, sets the input position indicator to absolute (relative to the
   beginning of the file) value pos. Specifically, executes rdbuf()->pubseekpos(pos,
   std::ios_base::in) (pubseekpos, in turn, calls seekpos of the specific buffer, such
   as basic_filebuf::seekpos, basic_stringbuf::seekpos, or strstreambuf::seekpos). In
   case of failure, calls setstate(std::ios_base::failbit).
   2) if fail() != true, sets the input position indicator to position off, relative to
   position, defined by dir. Specifically, executes rdbuf()->pubseekoff(off, dir,
   std::ios_base::in). In case of failure, calls setstate(std::ios_base::failbit).

.SH Parameters

   pos - absolute position to set the input position indicator to
   off - relative position (positive or negative) to set the input position indicator
         to
         defines base position to apply the relative offset to. It can be one of the
         following constants:

   dir - Constant Explanation
         beg      the beginning of a stream
         end      the ending of a stream
         cur      the current position of stream position indicator

.SH Return value

   *this

.SH Exceptions


   failure if an error occurred (the error state flag is not goodbit) and exceptions()
   is set to throw for that state.

   If an internal operation throws an exception, it is caught and badbit is set. If
   exceptions() is set for badbit, the exception is rethrown.

.SH Notes

   seekg(n) is not necessarily equivalent to seekg(n, ios::beg). std::basic_ifstream,
   for example, requires the absolute position n to come from tellg().

.SH Example


// Run this code

 #include <iostream>
 #include <sstream>
 #include <string>

 int main()
 {
     std::string str = "Hello, world";
     std::istringstream in(str);
     std::string word1, word2;

     in >> word1;
     in.seekg(0); // rewind
     in >> word2;

     std::cout << "word1 = " << word1 << '\\n'
               << "word2 = " << word2 << '\\n';
 }

.SH Output:

 word1 = Hello,
 word2 = Hello,

   Defect reports

   The following behavior-changing defect reports were applied retroactively to
   previously published C++ standards.

     DR    Applied to         Behavior as published               Correct behavior
   LWG 129 C++98      there was no way to indicate a failure sets failbit on failure
   LWG 136 C++98      seekg could set the output stream      only sets the input stream
   LWG 537 C++98      the type of off was off_type&          corrected to off_type

.SH See also

   tellg      returns the input position indicator
              \fI(public member function)\fP
   tellp      returns the output position indicator
              \fI(public member function of std::basic_ostream<CharT,Traits>)\fP
   seekp      sets the output position indicator
              \fI(public member function of std::basic_ostream<CharT,Traits>)\fP
   pubseekpos invokes seekpos()
              \fI(public member function of std::basic_streambuf<CharT,Traits>)\fP
   seekpos    repositions the file position, using absolute addressing
   \fB[virtual]\fP  \fI(virtual protected member function of std::basic_filebuf<CharT,Traits>)\fP

              repositions the next pointer in the input sequence, output sequence, or
   seekpos    both using absolute addressing
   \fB[virtual]\fP  \fI\fI(virtual protected member function\fP of\fP
              std::basic_stringbuf<CharT,Traits,Allocator>)
   seekpos    repositions the next pointer in the input sequence, output sequence, or
   \fB[virtual]\fP  both using absolute addressing
              \fI(virtual protected member function of std::strstreambuf)\fP
   pubseekoff invokes seekoff()
              \fI(public member function of std::basic_streambuf<CharT,Traits>)\fP
   seekoff    repositions the file position, using relative addressing
   \fB[virtual]\fP  \fI(virtual protected member function of std::basic_filebuf<CharT,Traits>)\fP

              repositions the next pointer in the input sequence, output sequence, or
   seekoff    both, using relative addressing
   \fB[virtual]\fP  \fI\fI(virtual protected member function\fP of\fP
              std::basic_stringbuf<CharT,Traits,Allocator>)
   seekoff    repositions the next pointer in the input sequence, output sequence, or
   \fB[virtual]\fP  both, using relative addressing
              \fI(virtual protected member function of std::strstreambuf)\fP
